#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# Reason for this no-op: shellcheck disable=... before the first command disables the error for the
# entire script.
:

# Disable unused variable error (needed to keep track of version)
# shellcheck disable=SC2034
CMK_VERSION="2.5.0b1"

# Plugin for EMCs ScaleIO software. This plugins needs to be installed
# on all MDM servers as the information is provided only by an active
# MDM member. Also it is recommended to configure a cluster host with
# these members as nodes in Checkmk and apply the ScaleIO services to
# this cluster host.

# This plugin needs a configuration file in your MK_CONFDIR. This should
# be /etc/check_mk in most cases. An example of the mk_scaleio.cfg is
# the following:
#
# SIO_USER=myUser
# SIO_PASSWORD=myPassword

if [ ! "$MK_CONFDIR" ]; then
    echo "MK_CONFDIR not set!" >&2
    exit 1
fi

# shellcheck source=agents/cfg_examples/mk_scaleio.cfg
. "$MK_CONFDIR/mk_scaleio.cfg" || exit 1

if [ -z "${SIO_USER}" ] || [ -z "${SIO_PASSWORD}" ]; then
    echo "Please set SIO_USER and SIO_PASSWORD in $MK_CONFDIR/mk_scalio.cfg" >&2
    exit 1
fi

if type scli >/dev/null; then
    scli --login --username "$SIO_USER" --password "$SIO_PASSWORD" >/dev/null 2>&1

    if [ $? == 1 ]; then
        # Login fails if MDM server is not master. In this case we do
        # absolutely nothing but quit this script! The data will be
        # fetched elsewhere and we do not want to confuse Checkmk.
        exit 1
    fi

    # System
    echo '<<<scaleio_system>>>'
    scli --query_properties --object_type SYSTEM --all_objects --properties ID,NAME,CAPACITY_ALERT_HIGH_THRESHOLD,CAPACITY_ALERT_CRITICAL_THRESHOLD,MAX_CAPACITY_IN_KB,UNUSED_CAPACITY_IN_KB

    # MDM
    echo '<<<scaleio_mdm:sep(44)>>>'
    scli --query_cluster

    # SDS
    echo '<<<scaleio_sds>>>'
    scli --query_properties --object_type SDS --all_objects --properties ID,NAME,PROTECTION_DOMAIN_ID,STATE,MEMBERSHIP_STATE,MDM_CONNECTION_STATE,MAINTENANCE_MODE_STATE,MAX_CAPACITY_IN_KB,UNUSED_CAPACITY_IN_KB,

    # Volume
    echo '<<<scaleio_volume>>>'
    scli --query_properties --object_type VOLUME --all_objects --properties ID,NAME,SIZE,USER_DATA_READ_BWC,USER_DATA_WRITE_BWC

    # Protection Domain
    echo '<<<scaleio_pd>>>'
    scli --query_properties --object_type PROTECTION_DOMAIN --all_objects --properties ID,NAME,STATE,MAX_CAPACITY_IN_KB,UNUSED_CAPACITY_IN_KB

    # Storage Pool
    echo '<<<scaleio_storage_pool>>>'
    scli --query_properties --object_type STORAGE_POOL --all_objects --properties ID,NAME,MAX_CAPACITY_IN_KB,UNUSED_CAPACITY_IN_KB,FAILED_CAPACITY_IN_KB,TOTAL_READ_BWC,TOTAL_WRITE_BWC,REBALANCE_READ_BWC,REBALANCE_WRITE_BWC,

    # Device states
    echo '<<<scaleio_devices>>>'
    scli --query_properties --object_type DEVICE --all_objects --properties ID,SDS_ID,STORAGE_POOL_ID,STATE,ERR_STATE

    scli --logout >/dev/null 2>&1
fi
